package com.example.bankend.controllers;

import com.example.bankend.entity.Saving;
import com.example.bankend.form.CreateSaving;
import com.example.bankend.form.DeleteSaving;
import com.example.bankend.form.SelectAll;
import com.example.bankend.form.UpdateSaving;
import com.example.bankend.param.Result;
import com.example.bankend.service.SavingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author luolinila
 * @date 2025/6/18 09:49
 */
@RestController
@CrossOrigin
public class SavingController {
    @Autowired
    private SavingService savingService;

    @PostMapping("/saving/create")
    public Result<Saving> createSaving(@RequestBody CreateSaving form) {
        boolean inputTure = form.isTure();
        if (!inputTure) {
            return new Result<>(10000, "输入错误", null);
        }
        Long id = savingService.createSaving(form.getAdminID(), form.getAdminName(), form.getAdminPass(), form.getSavingName(), form.getDescrip());
        if (id == 10004) {
            return new Result<>(10004, "存款类型重复", null);
        }
        if (id == -1) {
            return new Result<>(10005, "添加失败", null);
        }
        Saving saving = savingService.getSavingById(id);
        if (saving == null) {
            return new Result<>(10006, "未知错误", null);
        }
        return new Result(200, "添加成功", saving);
    }

    @PostMapping("/saving/delete")
    public Result deleteSaving(@RequestBody DeleteSaving form) {
        boolean inputTure = form.isTure();
        if (!inputTure) {
            return new Result<>(10000, "输入错误", null);
        }
        Long res = savingService.deleteSaving(form.getAdminID(), form.getAdminName(), form.getAdminPass(), form.getSavingID());
        if (res == 10004) {
            return new Result(10004, "存款不存在", null);
        }
        if (res == -1) {
            return new Result(10005, "注销失败", null);
        }
        return new Result(200, "注销成功", null);
    }

    @PostMapping("/saving/getAll")
    public Result<List<Saving>> getAllSaving(@RequestBody SelectAll form) {
        boolean inputTure = form.isTure();
        if (!inputTure) {
            return new Result<>(10000, "输入错误", null);
        }
        List<Saving> res = savingService.getAllSaving(form.getAdminID(), form.getAdminName(), form.getAdminPass());
        if (res == null) {
            return new Result<>(10001, "查询失败", null);
        }
        return new Result<>(200, "查询成功", res);
    }

    @PostMapping("/saving/update")
    public Result<Saving> updateSaving(@RequestBody UpdateSaving form) {
        boolean isTure = form.isTure();
        if (!isTure) {
            return new Result<>(10000, "输入错误", null);
        }
        Long savingID = savingService.updateSaving(form.getAdminID(), form.getAdminName(), form.getAdminPass(), form.getSavingID(), form.getSavingName(), form.getDescrip());
        if (savingID == -1) {
            return new Result<>(10001, "修改失败", null);
        }
        if (savingID == 10004) {
            return new Result<>(10002, "存款类型不存在", null);
        }
        Saving saving = savingService.getSavingById(savingID);
        if (saving == null) {
            return new Result<>(10003, "未知错误", null);
        }
        return new Result<>(200, "修改成功", saving);
    }
}
